package ldkxingzhe.ldkxingzhedemo.tab;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.NotificationCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps.AMapException;
import com.amap.api.maps.MapView;
import com.amap.api.maps.offlinemap.OfflineMapCity;
import com.amap.api.maps.offlinemap.OfflineMapManager;
import com.amap.api.maps.offlinemap.OfflineMapProvince;
import com.amap.api.maps.offlinemap.OfflineMapStatus;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import ldkxingzhe.ldkxingzhedemo.R;


/**
 * Created by ke on 15-8-29.
 */
public class CityListFragment extends Fragment implements OfflineMapManager.OfflineMapDownloadListener {

    private final String TAG = CityListFragment.class.getSimpleName();

    private OfflineMapManager offlineMapManager;

    private EditText searchEditText;
    private LinearLayout searchResultLayout;
    private ExpandableListView expandableListView;
    private ListView listView;
    private View clearInput;

    CityListAdapter listAdapter;
    CityExpandableAdapter adapter;

    private Activity context;

    private List<OfflineMapProvince> offlineMapProvinceList = new ArrayList<>();
    private Map<Integer,List<OfflineMapCity>> cityMap = new HashMap<>();
    private List<OfflineMapCity> allCity = new ArrayList<>();
    private List<OfflineMapCity> searChResultList = new ArrayList<>();

    /** Notification */
    NotificationManager mNotificationManager;
    Notification mNotification;
    final static int notificationID = 100;

    private ProgressDialog initDialog;
    private final int UPDATE_LIST = 0;
    private final int DISMISS_INIT_DIALOG = 1;
    private final int SHOW_INIT_DIALOG = 2;
    private final int UPDATE_NOTIFICATION = 3;
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case UPDATE_LIST:
                    updateList();
                    break;
                case DISMISS_INIT_DIALOG:
                    initDialog.dismiss();
                    initData();
                    handler.sendEmptyMessage(UPDATE_LIST);
                    break;
                case SHOW_INIT_DIALOG:
                    if(initDialog != null){
                        initDialog.show();
                    }
                    break;
                case UPDATE_NOTIFICATION:
                    updateNotification(msg.arg1,msg.arg2);
                    break;
                default:
                    throw new IllegalStateException("shouldn't come here");
            }
        }
    };

    /**  正在下载的地图名称*/
    public String downloading = "";
    /** 暂停的列表 */
    public Set<String> pauseSet = new HashSet<>();
    /** 等待的列表 */
    public List<String> waitingList = new ArrayList<>();

    private OfflineMapDownloadManagerFragment mFragment;

    /**
     * 更新通知栏
     */
    private void updateNotification(int completeCode,int status) {
        Log.d(TAG, "updateNotification, " + completeCode);
        mNotification.contentView.setProgressBar(R.id.custom_progressbar, 100, completeCode, false);

        if(status == OfflineMapStatus.LOADING){
            mNotification.contentView.setTextViewText(R.id.tv_custom_progress_desc, "正在下载:" + completeCode + "%");
        }else if(status == OfflineMapStatus.UNZIP){
            mNotification.contentView.setTextViewText(R.id.tv_custom_progress_desc, "正在解压:" + completeCode + "%");
        }
        if(completeCode == 100 && status == OfflineMapStatus.LOADING) {
            Toast.makeText(context, "下载完成", Toast.LENGTH_SHORT).show();
            mNotification.flags = Notification.FLAG_AUTO_CANCEL;
            Log.d(TAG, "download completed");
            mNotification.contentView.setTextViewText(R.id.tv_custom_progress_desc,"下载完成");
        }
        mNotificationManager.notify(notificationID,mNotification);
    }

    private void initData() {

    }

    /**
     * 把一个省的对象转化为一个市的对象
     */
    public OfflineMapCity getCityByProvince(OfflineMapProvince aMapProvince) {
        OfflineMapCity aMapCity = new OfflineMapCity();
        aMapCity.setCity(aMapProvince.getProvinceName());
        aMapCity.setSize(aMapProvince.getSize());
        aMapCity.setCompleteCode(aMapProvince.getcompleteCode());
        aMapCity.setState(aMapProvince.getState());
        aMapCity.setUrl(aMapProvince.getUrl());
        return aMapCity;
    }

    private void updateList() {
        if(expandableListView != null && expandableListView.getVisibility() == View.VISIBLE){
            BaseExpandableListAdapter adapter = (BaseExpandableListAdapter)expandableListView.getExpandableListAdapter();
            if(adapter != null){
                Log.d(TAG,"update expandable list view");
                adapter.notifyDataSetChanged();
            }
        }

        if(listView != null && listView.getVisibility() == View.VISIBLE){
            BaseAdapter adapter = (BaseAdapter)listView.getAdapter();
            if(adapter != null){
                Log.d(TAG,"update list view");
                adapter.notifyDataSetChanged();
            }
        }

        List<OfflineMapCity> tmpList = offlineMapManager.getDownloadOfflineMapCityList();
        for(OfflineMapProvince mapProvince : offlineMapManager.getDownloadOfflineMapProvinceList()){
            tmpList.add(getCityByProvince(mapProvince));
        }

        for(OfflineMapCity city : allCity){
            String cityName = city.getCity();
            if(pauseSet.contains(cityName)){
                tmpList.add(city);
            }else if(waitingList.contains(cityName)){
                tmpList.add(city);
            }else if(downloading.equals(cityName)){
                tmpList.add(city);
            }
        }
        mFragment.updateResult(tmpList);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    /**
     * 初始化通知栏
     */
    private void showNotification(String cityName) {
        Log.d(TAG,"showNotification");
        mNotificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),R.layout.offlinedata_notification_view);
        remoteViews.setImageViewResource(R.id.custom_progress_icon, R.drawable.offline_notification_image);
        remoteViews.setTextViewText(R.id.tv_custom_progress_title, "安装" + cityName + "离线地图");
        remoteViews.setProgressBar(R.id.custom_progressbar, 100, 0, false);
        builder.setContent(remoteViews)
                .setWhen(System.currentTimeMillis())
                .setOngoing(true)
                .setSmallIcon(R.drawable.ic_notification)
                .setPriority(Notification.PRIORITY_DEFAULT);
        mNotification = builder.build();
        mNotification.flags = Notification.FLAG_ONGOING_EVENT;
        mNotificationManager.notify(notificationID,mNotification);
    }

    private void init() {
        try{
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // 此版本限制，使用离线地图，请初始化一个MapView
        MapView mapView = new MapView(context);
        offlineMapManager = new OfflineMapManager(context,this);
        offlineMapProvinceList = offlineMapManager.getOfflineMapProvinceList();
        allCity = offlineMapManager.getOfflineMapCityList();

        List<OfflineMapProvince> bigCityList = new ArrayList<>(); // 省,直辖市,全国缩略图. 做比较用
        List<OfflineMapCity> cityList = new ArrayList<>(); //以市格式保存直辖市、港澳、全国概要图
        List<OfflineMapCity> gangaoList = new ArrayList<OfflineMapCity>();// 保存港澳城市
        List<OfflineMapCity> gaiyaotuList = new ArrayList<OfflineMapCity>();// 保存概要图
        for (int i = 0; i < offlineMapProvinceList.size(); i++) {
            OfflineMapProvince offlineMapProvince = offlineMapProvinceList.get(i);
            List<OfflineMapCity> city = new ArrayList<OfflineMapCity>();
            OfflineMapCity aMapCity = getCityByProvince(offlineMapProvince);
            if (offlineMapProvince.getCityList().size() != 1) {
                city.add(aMapCity);
                city.addAll(offlineMapProvince.getCityList());
                allCity.add(aMapCity);
            } else {
                cityList.add(aMapCity);
                bigCityList.add(offlineMapProvince);
            }
            cityMap.put(i + 3, city);
        }

        OfflineMapProvince title = new OfflineMapProvince();

        title.setProvinceName("概要图");
        offlineMapProvinceList.add(0, title);
        title = new OfflineMapProvince();
        title.setProvinceName("直辖市");
        offlineMapProvinceList.add(1, title);

        title = new OfflineMapProvince();
        title.setProvinceName("港澳");
        offlineMapProvinceList.add(2, title);
        offlineMapProvinceList.removeAll(bigCityList);

        for (OfflineMapProvince aMapProvince : bigCityList) {
            if (aMapProvince.getProvinceName().contains("香港")
                    || aMapProvince.getProvinceName().contains("澳门")) {
                gangaoList.add(getCityByProvince(aMapProvince));
            } else if (aMapProvince.getProvinceName().contains("全国概要图")) {
                gaiyaotuList.add(getCityByProvince(aMapProvince));
            }
        }
/*        for (OfflineMapCity tmpCity : cityList){
            allCity.add(tmpCity);
        }*/
        try {
            cityList.remove(4);// 从List集合体中删除香港
            cityList.remove(4);// 从List集合体中删除澳门
            cityList.remove(4);// 从List集合体中删除澳门
        } catch (Throwable e) {
            e.printStackTrace();
        }
        cityMap.put(0, gaiyaotuList);// 在HashMap中第0位置添加全国概要图
        cityMap.put(1, cityList);// 在HashMap中第1位置添加直辖市
        cityMap.put(2, gangaoList);// 在HashMap中第2位置添加港澳
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        context = getActivity();
        initDialog = new ProgressDialog(context);
        initDialog.setMessage("正在获取离线城市列表");
        initDialog.setCancelable(false);
        initDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        initDialog.show();
        new Thread(){
            @Override
            public void run() {
                Looper.prepare();
                final Handler handler1 = new Handler();
                handler1.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        init();
                        handler.sendEmptyMessage(DISMISS_INIT_DIALOG);
                        handler.removeCallbacks(this);
                        Looper.myLooper().quit();
                    }
                }, 10);
                Looper.loop();
            }
        }.start();
        mFragment = (OfflineMapDownloadManagerFragment)((OffLineMapActivity)context).fragments[0];

        View view = inflater.inflate(R.layout.fragment_city_list,container,false);
        searchEditText = (EditText)view.findViewById(R.id.search_edit_text);
        searchEditText.addTextChangedListener(mTextWatcher);
        clearInput = view.findViewById(R.id.remove_input);
        clearInput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchEditText.setText("");
            }
        });
        clearInput.setVisibility(View.INVISIBLE);

        searchResultLayout = (LinearLayout)view.findViewById(R.id.linearlayout_search_result);
        listView = (ListView)view.findViewById(R.id.search_result_list_view);
        expandableListView = (ExpandableListView)view.findViewById(R.id.expandable_list_view);


        listAdapter = new CityListAdapter(CityListFragment.this,searChResultList);
        listView.setAdapter(listAdapter);
        searchResultLayout.setVisibility(View.GONE);
        adapter = new CityExpandableAdapter();
        expandableListView.setAdapter(adapter);
        expandableListView.setVisibility(View.VISIBLE);
        int width = context.getWindowManager().getDefaultDisplay().getWidth();
        expandableListView.setIndicatorBounds(width - 60, width - 10);
        return view;
    }

    private TextWatcher mTextWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            String result = s.toString();
            Log.d("TAG","afterTextChanged, string = " + result);
            if(TextUtils.isEmpty(result)){
                clearInput.setVisibility(View.INVISIBLE);
                expandableListView.setVisibility(View.VISIBLE);
                searchResultLayout.setVisibility(View.GONE);
            }else{
                clearInput.setVisibility(View.VISIBLE);
                expandableListView.setVisibility(View.GONE);
                searchResultLayout.setVisibility(View.VISIBLE);
                searChResultList.clear();
                for(OfflineMapCity tmpCity : allCity){
                    if(tmpCity.getCity().contains(result)){
                        searChResultList.add(tmpCity);
                    }
                }
                listAdapter.notifyDataSetChanged();
            }
        }
    };

    @Override
    public void onDownload(int status, int completeCode, String downName) {
        switch (status) {
            case OfflineMapStatus.SUCCESS:
                Log.d(TAG,"SUCCESS");
                downloading = "";
                if(waitingList.size() > 0){
                    Log.d(TAG,"downloading = " + downloading);
                    startNewDownload(waitingList.get(0));
                }else{
                    downloading = "";
                }
                break;
            case OfflineMapStatus.LOADING:
                Log.d("amap-download", "download: " + completeCode + "%" + ","
                        + downName);
                break;
            case OfflineMapStatus.UNZIP:
                Log.d("amap-unzip", "unzip: " + completeCode + "%" + "," + downName);
                break;
            case OfflineMapStatus.WAITING:
                break;
            case OfflineMapStatus.PAUSE:
                Log.d("amap-unzip", "pause: " + completeCode + "%" + "," + downName);
                break;
            case OfflineMapStatus.STOP:
                break;
            case OfflineMapStatus.ERROR:
                Log.e("amap-download", "download: " + " ERROR " + downName);
                break;
/*            case OfflineMapStatus.EXCEPTION_AMAP:
                Log.e("amap-download", "download: " + " EXCEPTION_AMAP " + downName);
                break;
            case OfflineMapStatus.EXCEPTION_NETWORK_LOADING:
                Log.e("amap-download", "download: " + " EXCEPTION_NETWORK_LOADING "
                        + downName);
                Toast.makeText(context, "网络异常", 1000).show();
                offlineMapManager.pause();
                break;
            case OfflineMapStatus.EXCEPTION_SDCARD:
                Log.e("amap-download", "download: " + " EXCEPTION_SDCARD "
                        + downName);
                break;*/
            default:
                break;
        }
        handler.sendEmptyMessage(UPDATE_LIST);
        Message message = new Message();
        message.arg1 = completeCode;
        message.arg2 = status;
        message.what = UPDATE_NOTIFICATION;
        handler.sendMessage(message);
    }
    public View initAdapterGetView(View convertView, ViewGroup parent, final OfflineMapCity tmpCity) {
        final CityItemViewHolder viewHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.offline_down_list_item_reconstruct, parent,false);
            viewHolder = new CityItemViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (CityItemViewHolder)convertView.getTag();
        }
        viewHolder.cityName.setText(tmpCity.getCity());
        viewHolder.mapSize.setText("地图" + String.valueOf(tmpCity.getSize() / 1024 / 1024f) + "M");

        // 当前离线地图状态
        int status = tmpCity.getState();
        final String tmpCityName = tmpCity.getCity();
        int complete = tmpCity.getcompleteCode();
        if(isProvince(tmpCityName)){
            OfflineMapProvince tmpProvince = offlineMapManager.getItemByProvinceName(tmpCityName);
            status = tmpProvince.getState();
            complete = tmpProvince.getcompleteCode();
        }
        switch (status){
            case OfflineMapStatus.SUCCESS:
                viewHolder.ivMap.setImageResource(R.drawable.offline_mapnavi_state_downloaded);
                viewHolder.downloaded.setVisibility(View.VISIBLE);
                viewHolder.downloaded.setText("已下载");
                viewHolder.download.setVisibility(View.GONE);
                break;
            case OfflineMapStatus.UNZIP:
                break;
            case OfflineMapStatus.WAITING:
                Log.d(TAG,"waiting");
                break;
            case OfflineMapStatus.PAUSE:
                Log.d(TAG,"cityname = " + tmpCity.getCity());
                Log.d(TAG,"status pause");
                break;
            case OfflineMapStatus.LOADING:
                if(complete > 0){
                    viewHolder.ivMap.setImageResource(R.drawable.offline_mapnavi_state_downloading);
                    viewHolder.download.setVisibility(View.GONE);
                    viewHolder.downloaded.setVisibility(View.VISIBLE);
                    if(tmpCityName.equals(downloading)){
                        viewHolder.downloaded.setText(OfflineMapLoadingStatus.DOWNLOADING.message);
                    }else if(waitingList.contains(tmpCityName)){
                        // 包含在等待列表中
                        viewHolder.downloaded.setText(OfflineMapLoadingStatus.WAITING.message);
                    }else{
                        viewHolder.downloaded.setText(OfflineMapLoadingStatus.PAUSING.message);
                        pauseSet.add(tmpCityName);
                    }
                }else{
                    if(waitingList.contains(tmpCityName)){
                        viewHolder.downloaded.setText(OfflineMapLoadingStatus.WAITING.message);
                    }else{
                        viewHolder.ivMap.setImageResource(R.drawable.offline_mapnavi_state_undownload);
                        viewHolder.downloaded.setVisibility(View.GONE);
                        viewHolder.download.setVisibility(View.VISIBLE);
                    }
                }
                break;
            default:
        }
        viewHolder.download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "start download cityName = " + tmpCityName);
                startNewDownload(tmpCityName);
            }
        });

        viewHolder.downloaded.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OfflineMapLoadingStatus status = null;
                for (OfflineMapLoadingStatus tmpStatus : OfflineMapLoadingStatus.values()) {
                    if (tmpStatus.message.equals(viewHolder.downloaded.getText().toString())) {
                        status = tmpStatus;
                        break;
                    }
                }
                if (status == null) {
                    throw new IllegalStateException("wrong happened, status = null");
                }
                switch (status) {
                    case DOWNLOADING:
                        offlineMapManager.pause();
                        viewHolder.downloaded.setText(OfflineMapLoadingStatus.PAUSING.message);
                        break;
                    case PAUSING:
                        startNewDownload(tmpCityName);
                        viewHolder.downloaded.setText(OfflineMapLoadingStatus.WAITING.message);
                        break;
                    case WAITING:
                        viewHolder.downloaded.setVisibility(View.GONE);
                        viewHolder.download.setVisibility(View.VISIBLE);
                        waitingList.remove(tmpCityName);
                        break;
                    case DOWNLOADED:
                        // Do nothing
                        break;
                    default:
                        break;
                }
                handler.sendEmptyMessage(UPDATE_LIST);
            }
        });

        if(status == OfflineMapStatus.LOADING
                || status == OfflineMapStatus.UNZIP
                || status == OfflineMapStatus.PAUSE
                || status == OfflineMapStatus.WAITING) {
            viewHolder.progressRL.setVisibility(View.VISIBLE);
            // 设置进度条
            int width = viewHolder.progressBackGround.getWidth();
            int resultWidth = width * complete / 100;
            ViewGroup.LayoutParams layoutParams = viewHolder.progress.getLayoutParams();
            layoutParams.width = resultWidth;
            viewHolder.progress.setLayoutParams(layoutParams);
            layoutParams = viewHolder.progressRL.getLayoutParams();
            layoutParams.width = resultWidth > width / 2 ? width / 2 : resultWidth;
            viewHolder.progressTextRL.setLayoutParams(layoutParams);
            viewHolder.progressText.setText(getStatusString(status, complete));
        }else{
            viewHolder.progressRL.setVisibility(View.GONE);
        }
        viewHolder.currentCity.setVisibility(View.GONE);

        final int tmpTatus = status;
        final int tmpComplete = complete;
        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if(tmpTatus != OfflineMapStatus.LOADING || tmpComplete > 0){
                    offlineMapManager.remove(tmpCityName);
                }
                viewHolder.downloaded.setText("下载中");
                viewHolder.downloaded.setVisibility(View.GONE);
                viewHolder.download.setVisibility(View.VISIBLE);
                viewHolder.ivMap.setImageResource(R.drawable.offline_mapnavi_state_undownload);
                handler.sendEmptyMessage(UPDATE_LIST);
                return false;
            }
        });

        return convertView;
    }
    // 根据名称, 判断是否是省
    public boolean isProvince(String cityName){
        List<String> provinceName = Arrays.asList(new String[]{"省","广西","宁夏","西藏","新疆","北京","上海","天津","重庆","香港","澳门"});
        for(String tmpProvince : provinceName){
            if(cityName.contains(tmpProvince)){
                return true;
            }
        }
        return false;
    }

    /**
     * 根据名称添加一个下载地图任务
     * @param name    城市名, 省名
     */
    private void startNewDownload(String name){
        if("".equals(downloading)){
            try{
                showNotification(name);
                downloading = name;
                if(isProvince(name)){
                    offlineMapManager.downloadByProvinceName(name);
                }else{
                    offlineMapManager.downloadByCityName(name);
                }
            } catch (AMapException e) {
                e.printStackTrace();
            }
        }else{
            if(waitingList.contains(name)){
                throw new IllegalStateException("waitingList should not contains name");
            }
            waitingList.add(name);
        }
        handler.sendEmptyMessage(UPDATE_LIST);
    }

    /**
     * 根据地图下载状态,和完成度, 合成显示数字
     * @param status      状态
     * @param complete    完成度 0-100
     * @return              合成显示的文字
     */
    private String getStatusString(int status, int complete) {
        String result = "";
        switch (status){
            case OfflineMapStatus.LOADING:
                // 下载中
                result = "下载中(" + complete + "%)";
                break;
            case OfflineMapStatus.ERROR:
                // 出错
                result = "出错";
                break;
            case OfflineMapStatus.PAUSE:
                // 暂停
                result = "暂停";
                break;
            case OfflineMapStatus.STOP:
                // 停止
                result = "停止";
                break;
            case OfflineMapStatus.CHECKUPDATES:
                // 检查更新
                result = "检查更新";
                break;
            case OfflineMapStatus.WAITING:
                // 等待中
                result = "等待中";
                break;
            case OfflineMapStatus.UNZIP:
                // 解压中
                result = "解压中(" + complete + "%)";
                break;
            case OfflineMapStatus.SUCCESS:
                // 成功
                result = "安装成功";
                break;
            default:
                throw new IllegalStateException("change your code, you forget some status");
        }
        return result;
    }


    class CityExpandableAdapter extends BaseExpandableListAdapter{

        @Override
        public int getGroupCount() {
            return offlineMapProvinceList.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return cityMap.get(groupPosition).size();
        }

        @Override
        public String getGroup(int groupPosition) {
            return offlineMapProvinceList.get(groupPosition).getProvinceName();
        }

        @Override
        public OfflineMapCity getChild(int groupPosition, int childPosition) {
            return cityMap.get(groupPosition).get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            if(convertView == null){
                convertView = LayoutInflater.from(context).inflate(R.layout.offline_group_title_layout_reconstruct,parent,false);
            }
            ((TextView)convertView.findViewById(R.id.text_title)).setText(getGroup(groupPosition));
            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            OfflineMapCity offlineMapCity =getChild(groupPosition,childPosition);
            convertView = initAdapterGetView(convertView,parent,offlineMapCity);
            CityItemViewHolder cityItemViewHolder = (CityItemViewHolder)convertView.getTag();
            if(getChild(groupPosition,childPosition).getCity().equals(getGroup(groupPosition))){
                cityItemViewHolder.currentCity.setText("(全省)");
                cityItemViewHolder.currentCity.setVisibility(View.VISIBLE);
            }
            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }
    }

}
